package com.neo.springcloud.controller;

import com.neo.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
//@DefaultProperties(defaultFallback = "defaultGlobalFallback")
public class OrderHystrixController {

    @Autowired
    private PaymentHystrixService paymentHystrixService;

    /**
     * 正常
     *
     * @param id
     * @return
     */
//    @HystrixCommand
    @GetMapping("consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        // int num = 10 / 0;
        String result = paymentHystrixService.paymentInfo_OK(id);
        log.info(">>>>> result:{}", result);
        return result;
    }


    /**
     * 异常
     *
     * @param id
     * @return
     */
    @GetMapping("consumer/payment/hystrix/timeout/{id}")
//    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod", commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
//    })
    public String paymentInfo_Timeout(@PathVariable("id") Integer id) {
        // 自身异常测试
        // int num = 10 / 0;
        String result = paymentHystrixService.paymentInfo_Timeout(id);
        log.info(">>>>> result:{}", result);
        return result;
    }

    public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id) {
        return "消费端:80 , 自身异常或远程系统繁忙返回的信息";
    }

    /**
     * 默认的全局 fallback 方法
     *
     * @return
     */
    public String defaultGlobalFallback() {
        return "全局异常处理信息，请稍后再试";
    }
}
